import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

// 后端控制器类
@RestController
public class ImageGeneratorController {

    // 接收前端传递的语句，并生成对应的图片
    @PostMapping("/generateImage")
    public ResponseEntity<byte[]> generateImage(@RequestBody String input) {
        // 调用AI模型或服务，将语句转换为图片的逻辑
        // 这里只是一个示例，实际实现需要根据具体的AI模型或服务进行调用

        // 假设生成的图片保存在本地
        File imageFile = new File("images/image.png");

        try {
            // 将图片读取为字节数组
            byte[] imageBytes = Files.readAllBytes(imageFile.toPath());

            // 返回图片字节数组
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG);
            return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
